import { Transform } from 'class-transformer';
import { IsNumber, IsOptional, Min } from 'class-validator';

import { IndexQueryDto } from './index-query.dto';

export class IndexDto<
  T extends Record<string, any> = Record<string, any>,
  K extends keyof T & string = string,
> extends IndexQueryDto<K> {
  @Transform(({ value }) => {
    if (typeof value === 'string' && value.trim() !== '') {
      return Number(value);
    }
    return value;
  })
  @IsNumber({}, { message: 'size必须是数字' })
  @IsOptional()
  @Min(1, { message: 'size 必须大于 1' })
  size?: number;

  @Transform(({ value }) => {
    if (typeof value === 'string' && value.trim() !== '') {
      value = Number(value) + 1;
    } else if (value < 0) {
      value = 1;
    } else {
      value = value + 1;
    }
    return value;
  })
  @IsNumber({}, { message: 'page必须是数字' })
  @IsOptional()
  page?: number;
}
